package com.ithao.controller;


import com.ithao.domain.Book1;
import com.ithao.service.BookService;
import com.ithao.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    //controller调service
    @Autowired
    @Qualifier("BookServiceImpl")
    BookService bookService;

    @RequestMapping("/allBook")
    public String allBook(Model model){
        List<Book1> list = bookService.quaryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }

    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String AddBook(Book1 book1){
        bookService.addBook(book1);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int id,Model model){
        Book1 book1=bookService.quaryBookByID(id);
        model.addAttribute("QBook",book1);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Book1 book1){
        bookService.updateBook(book1);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookService.deleteBookByID(id);
        return "redirect:/book/allBook";
    }

    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        Book1 book1 = bookService.queryBookByName(queryBookName);
        List<Book1> list=new ArrayList<Book1>();
        list.add(book1);

        if (book1==null){
            list=bookService.quaryAllBook();
            model.addAttribute("error","未查到");
        }

        model.addAttribute("list",list);
        return "allBook";
    }



}